
global user "`c(username)'"
global dirdata "C:\Users\\$user\Dropbox\CCT BJP\data"
global dir1  "$dirdata\Final data"
global dir2  "$dirdata\Results\graphs"
global dir3  "$dirdata\Results\"
cd "$dir3"

use "$dir1\pooled_hh.dta", clear
keep if tau>-5  
g age=age_m if hhmale==0
replace age=age_p if hhmale==1
g spanish=spanish_m if hhmale==0
replace spanish=spanish_p if hhmale==1

g Post=tau>=0
egen w_hoursw_head=rowtotal(w_hoursw_p w_hoursw_m)
g work_head=(work_m+work_p>0)



local nwork_lab "# of working adults"
local w_hoursw_t_lab "Hours/week"
local work_head_lab "Worked last week"
local w_hoursw_head_lab "Hours/week"
local work_m_lab "Worked last week"
local work_p_lab "Worked last week"
local w_hoursw_m_lab "Hours/week "
local w_hoursw_p_lab "Hours/week "
local parm Post
local replace replace

*** the one observartion with 1.5 years of schooling
replace schooling_m=1 if schooling_m==1.5
global covs1 "age_m spanish_m schooling_m age spanish schooling hhmale urban np nchild5 age_sac  c.age_sac#i.year c.np#i.year  i.departamento#i.year i.year"
 
local spec1  Post i.min_schooling  $covs1  

local sample1 "(min_schooling>=1 & min_schooling<9)"

local top1 "All"

probit work_m i.age_m i.schooling_m urban np hhmale spanish_m nchild5 age_sac elder cohab_m  age  schooling i.departamento if  year<2006 
predict wm_hat 
egen wmq3=xtile(wm_hat), nq(4)


local replace replace
foreach x in work_m  {
foreach y in 1 2 3 4  {
sum `x' [w=factor]  if tau==-1 & `sample1' & wmq3==`y'
local mean_dv=r(mean)
sum wm_hat [w=factor]  if tau==-1 & `sample1' & wmq3==`y'
local mean_wm=r(mean)
areg `x'  `spec1' [w=factor] if `sample1' & wmq3==`y' ,a(departamento) cluster(municipio)
estimates store r`x'1
outreg2 using "$dir3\PTable_wmq", excel `replace' ctitle(``x'_lab') keep(`parm') less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean P(work), `mean_wm', Mean DV, `mean_dv') alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}
}


******

use "$dir1\pooled_hh.dta", clear
keep if tau>-5  
g age=age_m if hhmale==0
replace age=age_p if hhmale==1
g spanish=spanish_m if hhmale==0
replace spanish=spanish_p if hhmale==1

g Post=tau>=0
egen w_hoursw_head=rowtotal(w_hoursw_p w_hoursw_m)
g work_head=(work_m+work_p>0)


local nwork_lab "# of working adults"
local w_hoursw_t_lab "Hours/week"
local work_head_lab "Worked last week"
local w_hoursw_head_lab "Hours/week"
local work_m_lab "Worked last week"
local work_p_lab "Worked last week"
local w_hoursw_m_lab "Hours/week "
local w_hoursw_p_lab "Hours/week "
local parm Post
local replace append


global covs1 "age_m spanish_m schooling_m age spanish schooling hhmale urban np nchild5 age_sac  c.age_sac#i.year c.np#i.year  i.departamento#i.year i.year"
 
local spec1  Post i.min_schooling  $covs1  

local sample1 "(min_schooling>=1 & min_schooling<9)"

local top1 "All"


probit work_p i.age_p i.schooling_p urban np hhmale spanish_m nchild5 age_sac elder cohab_m  age  schooling i.departamento if  year<2006 
predict wp_hat 
egen wpq3=xtile(wp_hat), nq(4) 

foreach x in work_p  {
foreach y in 1 2 3  4{
sum `x' [w=factor]  if tau==-1 & `sample1' & wpq3==`y'
local mean_dv=r(mean)
sum wp_hat [w=factor]  if tau==-1 & `sample1' & wpq3==`y'
local mean_wp=r(mean)
areg `x'  `spec1' [w=factor] if `sample1' & wpq3==`y' ,a(departamento) cluster(municipio)
estimates store r`x'1
outreg2 using "$dir3\PTable_wmq", excel `replace' ctitle(``x'_lab') keep(`parm') less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean P(work), `mean_wp',Mean DV, `mean_dv') alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}
}



****** Panel B - Ypercap


global user "`c(username)'"
*global user "sebastiane"
global dirdata "C:\Users\\$user\Dropbox\CCT BJP\data"
global dir1  "$dirdata\Final data"
global dir2  "$dirdata\Results\graphs"
global dir3  "$dirdata\Results\"
cd "$dir3"
use "$dirdata\Raw data EH 2005-2009\dwelling.dta", clear
destring folio, replace
keep if year<2010
use "$dir1\pooled_hh.dta", clear
g age=age_m if hhmale==0
replace age=age_p if hhmale==1
g spanish=spanish_m if hhmale==0
replace spanish=spanish_p if hhmale==1
drop schooling
g schooling=schooling_m if hhmale==0
replace schooling=schooling_p if hhmale==1
joinby folio year using "$dirdata\Raw data EH 2005-2009\dwelling.dta" , unm(m)
tab _merge

gen wall_brick=wall==1
label var wall_brick "Wall is made of brick or cement"
gen wall_adobe=wall==2
label var wall_adobe  "Wall is made of adobe"
gen wall_wood=wall==5
label var wall_adobe  "Wall is made of wood"
gen roof_solid= roof==1 |roof==2 | roof==3
label var roof_solid "Roof is made of solid material/water proof material"
gen water_pipe=water==1 
label var water_pipe "HH has drinking water connection through pipes"
***** Predictive model for hh percap income
g ly=log(yhogpc)
quietly: reg ly urban i.schooling indigena hhmale i.nchild5 i.elder i.nadults i.nschoolage age_sac i.municipio floor_finished fuel_organic wall_brick wall_wood wall_adobe roof_solid water_pipe if year<2006
predict yhat, xb

egen yhat_q=xtile(yhat), nq(4) 


g Post=tau>=0
egen w_hoursw_head=rowtotal(w_hoursw_p w_hoursw_m)
g work_head=(work_m+work_p>0)
egen w_costs_m=rowtotal(w_insumos_m w_salarios_m)
egen w_costs_p=rowtotal(w_insumos_p w_salarios_p)
replace w_costs_p=. if year==2002
replace w_costs_m=. if year==2002

g any_costs_p=w_costs_p>0
g any_costs_m=w_costs_m>0

replace any_costs_p=. if  w_costs_p==.
replace any_costs_m=. if w_costs_m==.

local nwork_lab "# of working adults"
local w_hoursw_t_lab "Hours/week"
local work_head_lab "Worked last week"
local w_hoursw_head_lab "Hours/week"
local work_m_lab "Worked last week"
local work_p_lab "Worked last week"
local w_hoursw_m_lab "Hours/week "
local w_hoursw_p_lab "Hours/week "
local parm Post
local replace replace

*** the one observartion with 1.5 years of schooling
replace schooling_m=1 if schooling_m==1.5
global covs1 "age_m spanish_m schooling_m age spanish schooling hhmale urban np nchild5 age_sac  c.age_sac#i.year c.np#i.year  i.departamento#i.year i.year"
 
local spec1  Post i.min_schooling  $covs1  

local sample1 "(min_schooling>=1 & min_schooling<9)"

local top1 "All"

local replace replace
foreach x in work_m {
foreach y in 1 2 3  4 {
sum `x' [w=factor]  if tau==-1 & `sample1' & yhat_q==`y'
local mean_dv=r(mean)
sum yhat [w=factor]  if tau==-1 & `sample1' & yhat_q==`y'
local mean_wm=r(mean)
areg `x'  `spec1' [w=factor] if `sample1' & yhat_q==`y' ,a(departamento) cluster(municipio)
estimates store r`x'1
outreg2 using "$dir3\PTable_yq", excel `replace' ctitle(``x'_lab') keep(`parm') less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean P(work), `mean_wm', Mean DV, `mean_dv') alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}
}

global covs1 "age_p spanish_p schooling_p age spanish schooling hhmale urban np nchild5 age_sac  c.age_sac#i.year c.np#i.year  i.departamento#i.year i.year"
 
local spec1  Post i.min_schooling  $covs1 
local sample1 "(min_schooling>=1 & min_schooling<9)"

local top1 "All" 
foreach x in work_p  {
foreach y in 1 2 3  4 {
sum `x' [w=factor]  if tau==-1 & `sample1' & yhat_q==`y'
local mean_dv=r(mean)
sum yhat [w=factor]  if tau==-1 & `sample1' & yhat_q==`y'
local mean_wm=r(mean)
areg `x'  `spec1' [w=factor] if `sample1' & yhat_q==`y' ,a(departamento) cluster(municipio)
estimates store r`x'1
outreg2 using "$dir3\PTable_yq", excel `replace' ctitle(``x'_lab') keep(`parm') less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean P(work), `mean_wm', Mean DV, `mean_dv') alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}
}

